<?php

namespace App\Models;

use App\Services\CommonService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Services\GoodsService;

class GoodsHuabei extends Model
{
    use HasFactory;

    protected $table = 'sp_goods_huabei';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //花呗用户手续费
    const HUABEI_USER_FEE_BEAR_LIST = [
        3 => 2.30,
        6 => 4.50,
        12 => 7.50,
    ];

    //花呗商户手续费
    const HUABEI_SHOP_FEE_BEAR_LIST = [
        3 => 1.80,
        6 => 4.50,
        12 => 7.50,
    ];

    //获取租期列表
    protected function GetHuabeiList($setmeal_id){
        $data = $this->where([['setmeal_id',$setmeal_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('id','setmeal_id','lease_term','rent_total','three_bear','three_fee','six_bear','six_fee','twelve_bear','twelve_fee')->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //小程序商品详情获取花呗信息
    protected function GetHuabeiData($goods_id,$setmeal_id,$spec_id){
        $data = $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['spec_id',$spec_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('id','lease_term','rent_total','three_bear','three_fee','six_bear','six_fee','twelve_bear','twelve_fee')->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //获取花呗手续费金额
    protected function GetHuabeiFee($goods_id,$setmeal_id,$spec_id,$huabei_stages,$number){
        $huabei = $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['spec_id',$spec_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('rent_total','three_bear','three_fee','six_bear','six_fee','twelve_bear','twelve_fee')->first();
        $huabei = $huabei ? $huabei->toArray() : [];
        $stages_fee = 0;
        if($huabei_stages == 3 && !empty($huabei['three_bear'])){
            if($huabei['three_bear'] == GoodsService::GOODS_HUABEI_FEE_MERCHANT){
                $stages_fee = 0;
            }elseif ($huabei['three_bear'] == GoodsService::GOODS_HUABEI_FEE_USER){
                $stages_fee = $huabei['three_fee'] * $number;
            }
        }elseif($huabei_stages == 6 && !empty($huabei['six_bear'])){
            if($huabei['six_bear'] == GoodsService::GOODS_HUABEI_FEE_MERCHANT){
                $stages_fee = 0;
            }elseif ($huabei['six_bear'] == GoodsService::GOODS_HUABEI_FEE_USER){
                $stages_fee = $huabei['six_fee'] * $number;
            }
        }elseif($huabei_stages == 12 && !empty($huabei['twelve_bear'])){
            if($huabei['twelve_bear'] == GoodsService::GOODS_HUABEI_FEE_MERCHANT){
                $stages_fee = 0;
            }elseif ($huabei['twelve_bear'] == GoodsService::GOODS_HUABEI_FEE_USER){
                $stages_fee = $huabei['twelve_fee'] * $number;
            }
        }
        return $stages_fee;
    }

    //获取花呗手续费承担方
    protected function getHuabeiFeeBear($goods_id,$setmeal_id,$spec_id,$huabei_stages){
        $huabei = $this->select('rent_total','three_bear','three_fee','six_bear','six_fee','twelve_bear','twelve_fee')->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['spec_id',$spec_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->first();
        $huabei = $huabei ? $huabei->toArray() : [];
        if($huabei_stages == 3 && !empty($huabei['three_bear'])){
            $huabei_fee_bear = $huabei['three_bear'];
        }elseif($huabei_stages == 6 && !empty($huabei['six_bear'])){
            $huabei_fee_bear = $huabei['six_bear'];
        }elseif($huabei_stages == 12 && !empty($huabei['twelve_bear'])){
            $huabei_fee_bear = $huabei['twelve_bear'];
        }
        return $huabei_fee_bear;
    }



    //添加花呗信息
    protected function AddHuabei($goods_id,$setmeal_id,$huabei_data,$spec_id){
        foreach ($huabei_data as $k=>$v){
            if(!empty($v['lease_term']) && !empty($v['rent_total'])){
                $data = [];
                $data['goods_id'] = $goods_id;
                $data['setmeal_id'] = $setmeal_id;
                $data['spec_id'] = $spec_id;
                $data['lease_term'] = $v['lease_term'];
                $data['rent_total'] = $v['rent_total'];
                $data['three_bear'] = !empty($v['three_bear'])?$v['three_bear']:'';
                $data['three_fee'] = !empty($v['three_fee'])?$v['three_fee']:'';
                $data['six_bear'] = !empty($v['six_bear'])?$v['six_bear']:'';
                $data['six_fee'] = !empty($v['six_fee'])?$v['six_fee']:'';
                $data['twelve_bear'] = !empty($v['twelve_bear'])?$v['twelve_bear']:'';
                $data['twelve_fee'] = !empty($v['twelve_fee'])?$v['twelve_fee']:'';
                $data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                $this->insert($data);
            }
        }
    }

    //添加花呗信息
    protected function EditHuabei($goods_id,$setmeal_id,$huabei_data,$spec_id){
        foreach ($huabei_data as $k=>$v){
            if(!empty($v['lease_term']) && !empty($v['rent_total'])){
                if(!empty($v['id'])){
                    $data = [];
                    $data['rent_total'] = $v['rent_total'];
                    $data['three_bear'] = !empty($v['three_bear'])?$v['three_bear']:'';
                    $data['three_fee'] = !empty($v['three_fee'])?$v['three_fee']:'';
                    $data['six_bear'] = !empty($v['six_bear'])?$v['six_bear']:'';
                    $data['six_fee'] = !empty($v['six_fee'])?$v['six_fee']:'';
                    $data['twelve_bear'] = !empty($v['twelve_bear'])?$v['twelve_bear']:'';
                    $data['twelve_fee'] = !empty($v['twelve_fee'])?$v['twelve_fee']:'';
                    $data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                    $this->where('id',$v['id'])->update($data);
                }else{
                    $data = [];
                    $data['goods_id'] = $goods_id;
                    $data['setmeal_id'] = $setmeal_id;
                    $data['spec_id'] = $spec_id;
                    $data['lease_term'] = $v['lease_term'];
                    $data['rent_total'] = $v['rent_total'];
                    $data['three_bear'] = !empty($v['three_bear'])?$v['three_bear']:'';
                    $data['three_fee'] = !empty($v['three_fee'])?$v['three_fee']:'';
                    $data['six_bear'] = !empty($v['six_bear'])?$v['six_bear']:'';
                    $data['six_fee'] = !empty($v['six_fee'])?$v['six_fee']:'';
                    $data['twelve_bear'] = !empty($v['twelve_bear'])?$v['twelve_bear']:'';
                    $data['twelve_fee'] = !empty($v['twelve_fee'])?$v['twelve_fee']:'';
                    $data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                    $this->insert($data);
                }
            }
        }
    }

}
